home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Tetris Light 1.0 / source / alert.c < prev    next >
Text File  |  1993-07-18  |  6KB  |  210 lines

  1. /**********************************************************************\
  2.  
  3. File:        alert.c
  4.  
  5. Purpose:    This module provides routines to display various messages
  6.             to the user in alerts.
  7.             
  8.  
  9. ``Tetris Light'' - a simple implementation of a Tetris game.
  10. Copyright (C) 1993 Hoylen Sue
  11.  
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16.  
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. GNU General Public License for more details.
  21.  
  22. You should have received a copy of the GNU General Public License
  23. along with this program; see the file COPYING.  If not, write to the
  24. Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  
  26. \**********************************************************************/
  27.  
  28. #include "local.h"
  29.  
  30. #include "alert.h"
  31. #include "dialutil.h"
  32. #include "resources.h"
  33. #include "windows.h"
  34.  
  35. /*--------------------------------------------------------------------*/
  36.  
  37. static pascal Boolean alert_filter_proc(DialogPtr dp, EventRecord *evt,
  38.                                         INTEGER *itemhit)
  39. /* Event filter procedure for the alert dialogs.  Recognizes the return
  40.    and enter key equivalents for the default button (all these dialogs
  41.    only have one active button.) */
  42. {
  43.     switch (evt->what) {
  44.     case keyDown:
  45.         if ((evt->message & charCodeMask) == RETURN_CODE ||
  46.             (evt->message & charCodeMask) == ENTER_CODE) {            
  47.             simulate_key_hit(dp, OK);
  48.             *itemhit = OK;
  49.             return TRUE;
  50.         }
  51.         break;
  52.     case activateEvt:
  53.         if (dp != (WindowPtr) evt->message) {
  54.             if (evt->modifiers & activeFlag)
  55.                 window_activate((WindowPtr) evt->message);
  56.             else
  57.                 window_deactivate((WindowPtr) evt->message);
  58.         }
  59.         break;
  60.     case updateEvt:
  61.         if (dp != (WindowPtr) evt->message)
  62.             window_update((WindowPtr) evt->message);
  63.         break;
  64.     }
  65.  
  66.     return FALSE;
  67. }
  68.  
  69. /*--------------------------------------------------------------------*/
  70.  
  71. void alert_caution(INTEGER msg_id)
  72. /* Displays an alert message to the user. Note that a dialog is used
  73.    rather than a real alert, because the alert dialog boxes supplied
  74.    by the ToolBox do not handle update events properly (i.e. their
  75.    icon and default button highlighting does not get redrawn. The
  76.    static text is set to the alert 'STR#' string with `msg_id' index If
  77.    `msg_id' is zero, the default message is used.  Note that these 
  78.    message strings may contain ParamText substitutes, which may be
  79.    filled in by calling ParamText before this. */
  80. {
  81.     DialogPtr    dial;
  82.     INTEGER        item;
  83.     
  84.     dial = GetNewDialog(CAUTION_DIAL_ID, NIL, (WindowPtr) -1);
  85.     install_hilight_button(dial, OK, CAUTION_DIAL_HIGHLIGHT_ITEM);
  86.  
  87.     /* Set up the message with parameters */
  88.     
  89.     if (msg_id != 0) {
  90.         INTEGER type;
  91.         Handle handle;
  92.         Rect box;
  93.         Str255 str;
  94.     
  95.         GetDItem(dial, CAUTION_DIAL_TEXT_ITEM, &type, &handle, &box);
  96.         GetIndString(str, CAUTION_ALERT_STRs_ID, msg_id);
  97.         if (*str != 0)
  98.             SetIText(handle, str);        
  99.     }
  100.  
  101.     /* Display the alert */
  102.     
  103.     SysBeep(5);
  104.     ModalDialog(alert_filter_proc, &item);
  105.     DisposDialog(dial);
  106. }
  107.  
  108. /*--------------------------------------------------------------------*/
  109.  
  110. void alert_erc(INTEGER msg_id, OSErr erc)
  111. /* Similar to `alert_caution' above, except an error code `erc' is
  112.    also reported. */
  113. {
  114.     DialogPtr    dial;
  115.     INTEGER        item;
  116.  
  117.     dial = GetNewDialog(CAUTION_ERC_DIAL_ID, NIL, (WindowPtr) -1);
  118.     install_hilight_button(dial, OK, CAUTION_ERC_DIAL_HIGHLIGHT_ITEM);
  119.  
  120.     /* Set up the message string and parameter text */
  121.     
  122.     if (msg_id != 0) {
  123.         INTEGER type;
  124.         Handle handle;
  125.         Rect box;
  126.         Str255 str;
  127.     
  128.         GetDItem(dial, CAUTION_ERC_DIAL_TEXT_ITEM, &type, &handle, &box);
  129.         GetIndString(str, CAUTION_ERC_ALERT_STRs_ID, msg_id);
  130.         if (*str != 0)
  131.             SetIText(handle, str);        
  132.     }
  133.  
  134.     /* Set up the error code text */
  135.     
  136.     if (erc != noErr) {
  137.         INTEGER type;
  138.         Handle handle;
  139.         Rect box;
  140.         Str255 code_string;
  141.     
  142.         GetDItem(dial, CAUTION_ERC_ERC_ITEM_NO, &type, &handle, &box);
  143.         NumToString(erc, code_string);
  144.         SetIText(handle, code_string);
  145.     }
  146.     
  147.     /* Display the dialog */
  148.     
  149.     SysBeep(5);
  150.     ModalDialog(alert_filter_proc, &item);
  151.     DisposDialog(dial);
  152. }
  153.  
  154. /*--------------------------------------------------------------------*/
  155.  
  156. static pascal Boolean fatal_filter_proc(DialogPtr dp, EventRecord *evt,
  157.                                         INTEGER *itemhit)
  158. /* Event filter procedure for the fatal alert dialogs.  Recognizes the
  159.    return and enter key equivalents for the default button, but for
  160.    other events do not process them normally, because the application
  161.    might be in such a bad state that they could crash. */
  162. {
  163.     switch (evt->what) {
  164.     case keyDown:
  165.         if ((evt->message & charCodeMask) == RETURN_CODE ||
  166.             (evt->message & charCodeMask) == ENTER_CODE) {            
  167.             simulate_key_hit(dp, OK);
  168.             *itemhit = OK;
  169.             return TRUE;
  170.         }
  171.         break;
  172.     }
  173.  
  174.     return FALSE;
  175. }
  176.  
  177. /*--------------------------------------------------------------------*/
  178.  
  179. void alert_fatal(INTEGER msg_id)
  180. /* Emergency abort routine. Used when the program cannot continue 
  181.    any further (e.g. missing resources). The multiple beeps indicate
  182.    that it was a `controlled' abort, rather than an unexpected one. */
  183. {
  184.     DialogPtr    dial;
  185.     INTEGER        item;
  186.     
  187.     dial = GetNewDialog(FATAL_DIAL_ID, NIL, (WindowPtr) -1);
  188.     install_hilight_button(dial, OK, FATAL_DIAL_HIGHLIGHT_ITEM);
  189.  
  190.     if (msg_id != 0) {
  191.         INTEGER type;
  192.         Handle handle;
  193.         Rect box;
  194.         Str255 str;
  195.     
  196.         GetDItem(dial, FATAL_DIAL_TEXT_ITEM, &type, &handle, &box);
  197.         GetIndString(str, FATAL_ALERT_STRs_ID, msg_id);
  198.         if (*str != 0)
  199.             SetIText(handle, str);        
  200.     }
  201.  
  202.     SysBeep(5);
  203.     ModalDialog(fatal_filter_proc, &item);
  204.     DisposDialog(dial);
  205.     
  206.     ExitToShell();
  207. }
  208.  
  209. /*--------------------------------------------------------------------*/
  210.